package com.sunny.ynhz.ui.patrol.problems.draft;

import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Bundle;

import com.amap.api.maps.AMap;
import com.amap.api.maps.CameraUpdateFactory;
import com.amap.api.maps.MapView;
import com.amap.api.maps.model.BitmapDescriptorFactory;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.LatLngBounds;
import com.amap.api.maps.model.MarkerOptions;
import com.amap.api.maps.model.PolylineOptions;
import com.sunny.ynhz.R;
import com.sunny.ynhz.bean.PatrolBean;
import com.sunny.ynhz.db.RealmUtils;
import com.sunny.ynhz.ui.base.AppBaseTranslationTitleBarActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.realm.Realm;
import io.realm.RealmChangeListener;
import io.realm.RealmResults;

/**
 * Created by luao on 2017/10/24.
 */

public class PatrolPathActivity extends AppBaseTranslationTitleBarActivity {
    @BindView(R.id.patrol_path_map)
    MapView patrolPathMap;
    private AMap aMap;
    private Realm mRealm;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_patrol_path);
        ButterKnife.bind(this);
        patrolPathMap.onCreate(savedInstanceState);// 此方法必须重写
        aMap = patrolPathMap.getMap();

        String id = getIntent().getStringExtra("id");
        mRealm = RealmUtils.getDeleteRealm();
        mRealm.where(PatrolBean.class).equalTo("id", id).findAllAsync().addChangeListener(new RealmChangeListener<RealmResults<PatrolBean>>() {
            @Override
            public void onChange(RealmResults<PatrolBean> patrolBeen) {
                List<LatLng> latLngs = new ArrayList<>();
                int count = patrolBeen.first().getLocationBeanlist().size();
                LatLngBounds.Builder latlngBounds = new LatLngBounds.Builder();
                for (int i = 0; i < count; i++) {
                    LatLng latLng1 = new LatLng(patrolBeen.first().getLocationBeanlist().get(i).getLttd(), patrolBeen.first().getLocationBeanlist().get(i).getLgtd());
                    latlngBounds.include(latLng1);
                    latLngs.add(latLng1);
                }
                aMap.addPolyline(new PolylineOptions().
                        addAll(latLngs).width(10).color(Color.RED));

                aMap.addMarker(
                        new MarkerOptions()
                                .position(latLngs.get(0))
                                .title("起点").snippet("起点")
                                .draggable(false)
                                .icon(BitmapDescriptorFactory.fromBitmap(BitmapFactory.decodeResource(getResources(), R.mipmap.begin_marker)))
                                .setFlat(true));

                aMap.addMarker(
                        new MarkerOptions()
                                .position(latLngs.get(count - 1))
                                .title("终点").snippet("终点")
                                .draggable(false)
                                .icon(BitmapDescriptorFactory.fromBitmap(BitmapFactory.decodeResource(getResources(), R.mipmap.end_marker)))
                                .setFlat(true));
                //所有的点显示
                aMap.moveCamera(CameraUpdateFactory.newLatLngBounds(latlngBounds.build(),
                        15));
            }
        });

    }


}
